package com.boot.utils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @title FileUtil
 * @classname FileUtil
 * @author
 * @creatime
 * @description
 */
public class FileUtil {
	/**
	 *
	 * @description 多文件上传
	 * @param savePath
	 *            保存文件夹
	 * @param file
	 *            上传的文件
	 * @param request
	 *            请求
	 * @return
	 * @author ly
	 * @datetime 2018-10-31 下午8:18:53
	 */
	public List uploadFiles(String savePath, MultipartFile[] file, HttpServletRequest request) throws Exception{
		List urlList = new ArrayList();
		/*System.err.println("s上传文件地址---" + request.getSession().getServletContext().getRealPath(savePath));
		// 文件上传的绝对路径
		String path = request.getSession().getServletContext().getRealPath(savePath);*//*
		// 获取项目在tomcat的根目录
		String pathTomcat = request.getSession().getServletContext().getRealPath("");
		// 获取项目目录的上一级目录的路径
		System.err.println("项目在tomcat中的根目录"+pathTomcat);
		String path=pathTomcat.substring(0,pathTomcat.lastIndexOf(File.separator));*/



		File springBootpath = new File(ResourceUtils.getURL("classpath:").getPath());
		File upload = new File(springBootpath.getAbsolutePath());
		String path=upload.getAbsolutePath();
        System.err.println("服务器路径" + path);
		path= path.substring(0,path.lastIndexOf("/jiajiali/"));
		// 文件上传的绝对路径
		path = path + File.separator + savePath;
		System.err.println("存文件路径" + path);
		// 遍历文件
		for (MultipartFile files : file) {
			// 获取文件上传时的名字
			String fileName = files.getOriginalFilename();
			// 将文件名变更为唯一的UUID
			String filename = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
			// 获取保存的路径
			String saveFilePath = path + File.separator + filename;
			// 获取文件的路径
			String url = saveFilePath.substring(saveFilePath.indexOf(savePath));
			url = url.replace("\\", "/");
			// 将文件路径放入集合
			urlList.add(url);
			File dir = new File(path, filename);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			try {
				// MultipartFile自带的解析方法
				files.transferTo(dir);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return urlList;
	}

	/**
	 *
	 * @description 单文件上传
	 * @param savePath
	 *            保存文件夹
	 * @param file
	 *            上传的文件
	 * @param request
	 *            请求
	 * @return
	 * @author ly
	 * @datetime 2018-10-31 下午8:19:02
	 */
	public String uploadSingleFile(String savePath, MultipartFile file, HttpServletRequest request)throws Exception {
		/*// 获取项目在tomcat的根目录
		String pathTomcat = request.getSession().getServletContext().getRealPath("");
		// 获取项目目录的上一级目录的路径
		System.err.println("项目在tomcat中的根目录"+pathTomcat);
		String path=pathTomcat.substring(0,pathTomcat.lastIndexOf(File.separator));
		// 文件上传的绝对路径*/


		File springBootpath = new File(ResourceUtils.getURL("classpath:").getPath());
		File upload = new File(springBootpath.getAbsolutePath());
		String path=upload.getAbsolutePath();
		path= path.substring(0,path.lastIndexOf("jiajiali/"));
		path = path + File.separator + savePath;
		System.err.println("存文件路径" + path);
		// 遍历文件
		// 获取文件上传时的名字
		String filename = file.getOriginalFilename();
		// 将文件名变更为唯一的UUID

		//String filename = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
		// 获取保存的路径
		String saveFilePath = path + File.separator + filename;
		System.err.println("存文件路径" + saveFilePath);
		// 获取文件的路径
		String url = saveFilePath.substring(saveFilePath.indexOf(savePath));
		System.err.println("获取文件路径" + url);
		url = "https://jiajiani.cn/" + url.replace("\\", "/");
		System.err.println("数据库地址" + url);
		// 将文件路径放入集合
		File dir = new File(path, filename);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		try {
			// MultipartFile自带的解析方法
			file.transferTo(dir);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}

	public void changeImage(String imageurl,String imageinPath){
		try {
			//网络
			URL url = new URL(imageinPath);
			//本地
			InputStream imagein2 = new FileInputStream(
					imageurl);

			BufferedImage image =(BufferedImage)ImageIO.read(url);
			BufferedImage image2 = ImageIO.read(imagein2);
			//image2.getWidth() - 160, image2.getHeight() - 155,
			Graphics g = image.getGraphics();
			//g.drawImage(image2, 2630, 4350, 650,630,null);
			g.drawImage(image2, 420, 620, 110,110,null);
			OutputStream outImage = new FileOutputStream(
					imageurl);
			JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
			enc.encode(image);


			BufferedImage bimg=ImageIO.read(new FileInputStream(imageurl));
			//得到Graphics2D 对象
			Graphics2D g2d=(Graphics2D)bimg.getGraphics();
			//设置颜色和画笔粗细
			/*g2d.setColor(Color.black);
			g2d.setStroke(new BasicStroke(5));*/
			//String pathString = "D://qqfile/1852230493/FileRecv/SourceHanSansCN-/SourceHanSansCN-Heavy.otf";
//            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, new File(pathString));

			/*g2d.setFont(new Font("微软雅黑", Font.PLAIN, 36));

			//绘制图案或文字
			g2d.drawString("编号: "+i, 320, 700);*/


			ImageIO.write(bimg, "JPG",new FileOutputStream(imageurl));


			File fromFile = new File(imageurl);
			File toFile = new File(imageurl);

			//imagein.close();
			imagein2.close();
			outImage.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
